<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="教师编号">
              <a-input v-model="queryParam.gh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx && selectMenu.yx.length === 1" allowClear v-model="queryParam.dwh" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="教师类型">
                <a-select allowClear v-model="queryParam.jzglbm">
                  <a-select-option v-for="i in TYPE0049" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="审核状态">
                <a-select allowClear v-model="queryParam.shzt">
                  <a-select-option v-for="i in TYPE0318" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="10" :sm="20">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button type="primary" style="margin:0 10px" @click="exportExcel">导出</a-button>
            <a-button type="primary" style="margin:0 10px" @click="toAdd">创建教师</a-button>
            <a-button type="primary" v-hasPermission="'jzgxx:delete'" @click="batchDelete">批量删除</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :scroll="{ x: 1280 }"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="operation" slot-scope="a,b">
          <a @click="showDetail(b)">查看明细</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="toEdit(b)">修改</a>
              </a-menu-item>
              <a-menu-item v-hasPermission="'jzgxx:resetPasswd'">
                <a @click="resetPass(b)">重置密码</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import searchMixin from '@/utils/searchRemain'

export default {
  mixins: [ searchMixin ],
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          width: '70px',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '教师编号',
          // width: '120px',
          dataIndex: 'gh'
        },
        {
          title: '姓名',
          width: '120px',
          dataIndex: 'xm'
        },
        {
          title: '所在院系',
          // width: '220px',
          dataIndex: 'ssyxmc'
        },
        {
          title: '教师类型',
          width: '120px',
          dataIndex: 'jzglbmc'
        },
        {
          title: '职称',
          width: '120px',
          dataIndex: 'zcmc'
        },
        {
          title: '状态',
          width: '120px',
          dataIndex: 'shztmc'
        },
        {
          title: '导师类别',
          width: '150px',
          dataIndex: 'dslbmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '160px',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.teacherList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0049: state => state.app['TYPE0049'],
      TYPE0318: state => state.app['TYPE0318']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0049')
    this.$store.dispatch('app/setDictionary', 'TYPE0318')
    // this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'dwh', this.selectMenu.yx[0].dwh)
        }
        // this.$nextTick(() => {
        this.initDone = true
        //   // this.$refs.table.refresh(true)
        // })
      })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的教师')
        return
      }
      this.$confirm({
        title: `确定要将删除所选中教师吗`,
        content: '当您点击确定按钮后，这些教师将被删除',
        centered: true,
        onOk: () => {
          this.$api.cultivate.deleteJzg(this.selectedRowKeys).then(res => {
            this.$message.success(res.msg)
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    showDetail (t) {
      this.$router.push(`/cultivate/teacher/info/${t.gh}`)
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    toAdd () {
      this.$router.push(`/cultivate/teacher/add`)
    },
    toEdit (t) {
      this.$router.push(`/cultivate/teacher/add?gh=${t.gh}`)
    },
    resetPass (row){
      this.$confirm({
        title: '提示',
        content: `确定对教职工：${row.xm}，重置密码吗？（重置后密码为123456）`,
        okText: '确认',
        cancelText: '取消',
        onOk:() => {
          let params = {gh: row.gh};
          this.$api.student.resetPasswdByTea(params).then(res => {
            this.$message.success('操作成功')
          })
        },
        onCancel:() => {},
      })
    },
    exportExcel(){
      this.$export('foster/jzgxx/exportList',this.queryParam)
    }
  }
}
</script>

<style>

</style>
